|
|
|
| |
|
|
|
|
However, this does not work. In Visual Basic, all objects must be created using the Set keyword. |
|
|
|
|
|
|
|
|
|
|
The code in Listing 3.3 is all that you need to open an existing Microsoft Access database and create a Dynaset-type Recordset ready for update. However, for this project, you want to see a bit more. Let's add some code that tells you how many records are in the Titles data table. |
|
|
|
|
|
|
|
|
You need one more variable to hold the record count. You also use the MoveLast method to move the record pointer to the last record in the Recordset. This forces Visual Basic to touch every record in the collection, and therefore gives you an accurate count of the total number of records in the table. You get the count by reading the RecordCount property of the Recordset. When you have all that, you display a Visual Basic message box that tells you how many records are in the Recordset. Listing 3.4 contains the code to add. |
|
|
|
|
|
|
|
|
Listing 3.4. Counting the records in a Dynaset. |
|
|
|
|
|
|
|
|
Private Sub Form_Load()
'
' creating dynaset-type recordsets
'
Dim db As Database ' the database object
Dim rs As Recordset ' the recordset object
'
' create local variables
Dim strDBName As String
Dim strRSName As String
Dim intRecs As Integer
'
' initialize the variables
strDBName = App.Path & "\..\data\books5.mdb"
strRSName = "Titles"
'
' create the objects
Set db = DBEngine.OpenDatabase(strDBName)
Set rs = db.OpenRecordset(strRSName, dbOpenDynaset)
'
' count the records in the collection
rs.MoveLast ' move to end of list to force a count
intRecs = rs.RecordCount ' get count
MsgBox strRSName & " :" & CStr(intRecs), vbInformation, "Total Records in
Set"
'
End Sub |
|
|
|
|
|